import java.awt.Point;
/**
 * 
 * @author Shawn Gibbs
 * @version v0.1
 *
 */
public class Ship {
	public Ship(String name, int size){
		NAME = name;
		SIZE = size;
		spots = new Point[size];
		deck = new boolean[size];
		for(int i = 0; i < size; i++){
			deck[i] = true; 
			spots[i] = new Point(-1,-1);
		}
		
		/*
		 * Sets up the points array and Deck array
		 * by default all of Deck is true
		 * by default all of the points are -1, -1;
		 */
	}
	
	/**1
	 * This simply sets up the Spot array in the direction. 
	 * @param p - this is the front of the ship.
	 * @param direction - this is the direction point the user/computer choose.
	 * @throws DiagonalException - this if the Direction is diagonal isntead of being horizontal or verticle
	 */
	public void setupShip(Point p, Point direction) throws DiagonalException, ShipOutOfBoundsException{
		int x = p.x;
		int y = p.y;
		int s = direction.x;
		int t = direction.y;
		if(x == s){
			for(int i = 0; i < SIZE; i++) {
				//System.out.println("X:"+x+",Y+i:"+ (y+i));
				if(t >= y) spots[i].setLocation(x, y+i); //= new Point(x,y+i);
				else spots[i].setLocation(x, y-i);// = new Point(x,y-i);
			}
		}
		else if(y == t) {
			for(int i = 0; i < SIZE; i++) {
				if(s>=x) spots[i].setLocation(x+i, y); //= new Point(x + i,y);
				else spots[i].setLocation(x-i, y); //= new Point(x-i, y);
			}
		}
		else throw new DiagonalException();
		
		// This checks to see if the any point of the ship is out of bounds 
		for(int i = 0; i < SIZE; i++){
			int n = spots[i].x;
			int m = spots[i].y;
			if((n < 0 || n >= Board.BOARD_SIZE) || (m < 0 || m > Board.BOARD_SIZE)) throw new ShipOutOfBoundsException();
		}
	}
	/**
	 * This loops through Spots to see if 
	 * this spot exist, unlike fireAt this
	 * does not actually check anything, this
	 * is simply to check to see if there
	 * is something there
	 * @return true if there p and a spot matches, else return false
	 */
	public Point[] getPoints(){
		return spots;
	}
	public boolean isASpot(Point p){
		int x = p.x;
		int y = p.y;
		for(int i = 0; i < SIZE; i++){
			int s = spots[i].x;
			int t = spots[i].y;
			if( (x == s ) && (y==t)) return true;
		}
		/*for(int i = 0; i < SIZE; i++) {
			System.out.println("isASpot:"+i);
			int s = spots[i].x;
			int t = spots[i].y;
			if((x == s) && (y == t)) System.out.println("CLASH"); return true;
		}*/
		return false;
	}
	public boolean isDead() {
		return dead;
	}
	/**
	 * This looks through Spots and see if any of the spots
	 * are p
	 * If it is true you also want to make sure you set the 
	 * coorisponding location to hit. 
	 * @param p - The location you are firing at.
	 * @return true if it is a hit, false if its a miss
	 */
	public boolean fireAt(Point p){
		if(dead){
			return false; 
		}
		int x = p.x;
		int y = p.y;

		System.out.println("x:"+x+",y:"+y);
		
		boolean hit = false;
		int killCounter = 0;
		for(int i = 0; i < SIZE; i++) {
			int s = spots[i].x;
			int t = spots[i].y;
			if((x == s) && (y == t)){
				hit = true;
				deck[i] = false;
			}
			// We will need to check to see if the ship is dead or not.
			if(deck[i] == false){
				killCounter++;
			}
		}
		
		// If the ship is dead then we set is dead to true;
		if(killCounter == SIZE){
			dead = true;
		}
		return hit;
	}
	
	public final String NAME;
	public final int SIZE;
	
	/*
	 * If the all of deck is destroyed
	 * then the ship is dead AKA dead = true;
	 * else false;
	 */
	private boolean dead;
	
	/**
	 * Where all of the spots are at, 
	 * by default the spots will be off the board 
	 * till the player/computer sets up the ships
	 * 
	 */
	private Point[] spots;
	/**
	 * True =  The deck is there
	 * False = The deck has been blown up.
	 */
	private boolean[] deck;
	
	public static void main(String[] args) {

		Ship ship = new Ship("Battle Ship", 5);
		try {
			ship.setupShip(new Point(5,5),new Point(5,9));
			for(int i = 0; i < ship.SIZE; i++) {
				System.out.println("Firing at- " + ship.fireAt(new Point(5,5+i)));
				System.out.println("Is Dead? " + ship.isDead());
			}
		}
		catch (Exception e){
	
		}
	}
}
